﻿
using Microsoft.SqlServer.Server;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using 日铁.Model;
using 日铁.UI.SqlCe;
using Kingdee.BOS;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Kingdee.BOS.Util;

namespace 日铁
{
    public partial class FrmLandoWks : Form
    {
        public FrmLandoWks()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SqlLongin sqlLongin = new SqlLongin();
            sqlLongin.Show();
        }

        private void FrmLandoWks_Load(object sender, EventArgs e)
        {
            //更换IP和数据库
            //查询管理中心
            //连接对应数据中心
            var connectionString =
               $@"Data Source={ZhidingY.SQLIP};Initial Catalog={ZhidingY.SQLName};Persist Security Info=True;User ID={ZhidingY.SQLK};Password={ZhidingY.SQLmm}";
            try
            {
                var conn = new System.Data.SqlClient.SqlConnection(connectionString);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    var sql1 = $@"select FDATABASENAME,FNUMBER from T_BAS_DATACENTER ";
                    var sql2 = new SqlCommand(sql1,conn);
                    SqlDataReader D=sql2.ExecuteReader();
                    while (D.Read())
                    {
                        var hu = D["FDATABASENAME"].ToString();
                        var hu1 = D["FNUMBER"].ToString();
                        ArrayList arrayList = new ArrayList();
                        arrayList.Add(new DictionaryEntry(hu, hu1));
                        comboBox1.DataSource = arrayList;
                        comboBox1.DisplayMember = "Value";
                        comboBox1.ValueMember = "key";
                        comboBox1.SelectedIndex = 0;
                    }
                    // 连接成功
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("连接到数据库失败！");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var sql = this.comboBox1.SelectedValue.ToString();
            var sql1 = this.textBox2.Text;
            var sql2 = this.textBox3.Text;
            //密码验证
            var connectionString =
              $@"Data Source={ZhidingY.SQLIP};Initial Catalog={sql};Persist Security Info=True;User ID={ZhidingY.SQLK};Password={ZhidingY.SQLmm}";
            try
            {
                var conn = new System.Data.SqlClient.SqlConnection(connectionString);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    var QSL = $@"select FMEMO,FPASSWORD from T_SEC_USER where FNAME='"+ sql1 + "' ";
                    var KLM = new SqlCommand(QSL, conn);
                    SqlDataReader D = KLM.ExecuteReader();
                    while (D.Read())
                    {
                        var hu = D["FMEMO"].ToString();
                        var PAss = D["FPASSWORD"].ToString();
                        var DB=ConfidentialDataSecurityUtil.GenSecurityPW(sql2, hu);
                        if (DB == PAss)
                        {
                            MessageBox.Show("登录成功！");
                            //调转对应界面
                        }
                    }
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("连接到数据库失败！");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
    }
}
